<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="post" id="form">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="button" id="btn" value="提交">
    </form>
    <script>
        var btn = document.getElementById('btn');
        var form = document.getElementById('form');


        //为按钮添加点击事件
        btn.onclick = function () {
            //将html表单转化为formData对象
            var formData = new FormData(form);
            //获取表单中对象中属性的值
            var value = formData.get('username');
            console.log(value);
            //设置表单对象中属性的值
            formData.set('username', 'xqz');
            console.log(formData.get('username'));
            formData.append('username', 'syj');

            formData.delete('password');
            var xhr = new XMLHttpRequest();
            xhr.open('post', 'http://localhost:3000/formData');
            xhr.send(formData);
            xhr.onload = function () {
                //对http状态码进行判断
                if (xhr.status == 200) {
                    console.log(xhr.responseText);
                }

            }
            //创建空的表单对象
            var f = new FormData();
            f.append('sex', '男');
            console.log(f.get('sex'));
            console.log(f);
            

        }
    </script>
</body>

</html>